﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using NamAn_PPMv2.Models;

namespace NamAn_PPMv2.Controllers.Accounts
{
    public class AccountController : Controller
    {
        #region "Logon"
        [HttpGet]
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    HttpCookie ck = new HttpCookie("TenDangNhap");
                    ck.Value = model.UserName;
                    ck.Expires = DateTime.Now.AddDays(15);
                    Response.Cookies.Add(ck);

                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Tên đăng nhập hoặc mật khẩu không đúng.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        #endregion

        #region "Change Password"
        /// <summary>
        /// GET: /Account/ChangePassword
        /// </summary>
        /// <returns>View</returns>
        [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }

        /// <summary>
        /// POST: /Account/ChangePassword
        /// </summary>
        /// <param name="model"></param>
        /// <returns>View</returns>
        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                // ChangePassword will throw an exception rather
                // than return false in certain failure scenarios.
                bool changePasswordSucceeded;
                try
                {
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                    changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "Mật khẩu hiện tại không đúng hoặc mật khẩu mới không hợp lệ.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        #endregion

        [Authorize]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("LogOn", "Account");
        }

        /// <summary>
        /// GET: /Account/ChangePasswordSuccess
        /// </summary>
        [Authorize]
        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }
    }
}
